package org.video.mine.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.FlexboxLayout;

import org.greenrobot.eventbus.EventBus;
import org.video.mine.R;
import org.video.mine.Urls;
import org.video.mine.activity.PlayerActivity;
import org.video.mine.databinding.PlayListLayoutBinding;
import org.video.mine.entity.MoviePlayerEntity;
import org.video.mine.entity.PlayListEntity;
import org.video.mine.event.DownLoadAllEvent;
import org.video.mine.utils.ScreenUtils;

import java.util.List;

public class PlayListPlayAdapter extends RecyclerView.Adapter<PlayListPlayAdapter.ViewHolder> {

    List<PlayListEntity> listEntities;
    Context context;
    int which;
    int position;
    PlayerActivity activity;

    public PlayListPlayAdapter(List<PlayListEntity> listEntities, Context context, PlayerActivity activity) {
        this.listEntities = listEntities;
        this.context = context;
        this.activity = activity;
    }

    public int getWhich() {
        return which;
    }

    public void setWhich(int which) {
        this.which = which;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.play_list_layout, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        PlayListLayoutBinding binding = (PlayListLayoutBinding) holder.getTitleBinding();
        binding.tvDownAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DownLoadAllEvent(position));
            }
        });
        binding.setPlayList(listEntities.get(position));
        initFlexBox(binding,listEntities.get(position),position);
    }

    @Override
    public int getItemCount() {
        return listEntities.size();
    }

    private void initFlexBox(PlayListLayoutBinding binding, PlayListEntity playListEntity, int position){
        List<MoviePlayerEntity> playLists = playListEntity.getPlayLists();
        binding.flexItem.removeAllViews();
        for (int i=0; i<playLists.size(); i++){
            binding.flexItem.addView(createNewFlexItemTextView(binding,i,playLists,position));
        }
    }

    private TextView createNewFlexItemTextView(final PlayListLayoutBinding binding, final int index, final List<MoviePlayerEntity> playListEntity, final int nowposition) {
        final MoviePlayerEntity moviePlayerEntity = playListEntity.get(index);
        final TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setText(moviePlayerEntity.getName());
        textView.setTextSize(12);
        textView.setBackground(context.getResources().getDrawable(R.drawable.round));
        if (which == index && position == nowposition){
            textView.setTextColor(context.getResources().getColor(R.color.highLight));
            textView.setBackground(context.getResources().getDrawable(R.drawable.round_select));
        }else {
            textView.setTextColor(context.getResources().getColor(R.color.gray));
            textView.setBackground(context.getResources().getDrawable(R.drawable.round));
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (which != index || position!=nowposition){
                    activity.autoPlay = true;
                    activity.saveHistory();
                    TextView childAt = (TextView) binding.flexItem.getChildAt(which);
                    childAt.setTextColor(context.getResources().getColor(R.color.gray));
                    childAt.setBackground(context.getResources().getDrawable(R.drawable.round));

                    textView.setTextColor(context.getResources().getColor(R.color.highLight));
                    textView.setBackground(context.getResources().getDrawable(R.drawable.round_select));
                    activity.url = moviePlayerEntity.getUrl().startsWith("http")?moviePlayerEntity.getUrl():(Urls.getHomeUrl0() + moviePlayerEntity.getUrl());
                    activity.homeUrl = activity.url;
                    activity.loaded = false;
                    activity.stopPlay();
                    activity.currentPosition = 0;
                    activity.which = index;
                    activity.position = nowposition;
                    activity.getNetData();
                }
            }
        });
        ViewCompat.setPaddingRelative(textView, 30, 8, 30, 8);
        FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int margin = ScreenUtils.dp2px(6);
        int marginTop =ScreenUtils.dp2px(5);
        int bottom =ScreenUtils.dp2px(3);
        layoutParams.setMargins(margin, marginTop, margin, bottom);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding titleBinding = null;

        public ViewHolder(ViewDataBinding titleBinding) {
            super(titleBinding.getRoot());
            this.titleBinding = titleBinding;
        }

        public ViewDataBinding getTitleBinding(){
            return titleBinding;
        }
    }
}
